<?php

namespace App\Http\Controllers\api;

use App\Http\Controllers\Controller;
use App\Models\Record;
use App\Models\User;
use Illuminate\Http\Request;

class UserController extends CommonController
{
    public function login(Request $request)
    {
        $username = $request->input('username', '');
        $password = $request->input('password', '');
        $User = User::where('username', $username)->first();
        if (empty($User)) {
            return response()->json(['status' => '400', 'message' => '姓名错误'], 404, [], JSON_UNESCAPED_UNICODE);
        }
        if ($User->password != $password) {
            return response()->json(['status' => '400', 'message' => '密码错误'], 404, [], JSON_UNESCAPED_UNICODE);
        }
        return response()->json(['status' => '200', 'message' => '登录成功','username'=>$username,'password'=>$password], 200, [], JSON_UNESCAPED_UNICODE);
    }

    public   function  records(Request $request){
            $username = $request->input('username','');
            if (empty($username )){
                return response()->json(['status' => '400', 'message' => '没有姓名'], 400, [], JSON_UNESCAPED_UNICODE);
            }
            $email= $request->input('email','');
            if (empty($email )){
                return response()->json(['status' => '400', 'message' => '没有邮箱'], 400, [], JSON_UNESCAPED_UNICODE);
            }
            $mobile = $request->input('mobile','');
            if (empty($mobile)){
                return response()->json(['status' => '400', 'message' => '没有电话'], 400, [], JSON_UNESCAPED_UNICODE);
            }
            $role= $request->input('role','');
            if (empty($role)){
                return response()->json(['status' => '400', 'message' => '没有角色'], 400, [], JSON_UNESCAPED_UNICODE);
            }

            $data =[

                'username'=>$username,
                'email'=>$email,
                'mobile'=>$mobile,
                'role'=>$role,

            ];
            if (!Record::create($data)){
                return $this->response('400','添加失败');
            }
            return response()->json(['status' => '200', 'message' => '添加成功'], 200, [], JSON_UNESCAPED_UNICODE);

    }
    public function list()
    {
        $records = Record::all();

        return response()->json(['status' => '200', 'data' => $records], 200, [], JSON_UNESCAPED_UNICODE);
    }
}
